#include "TiledDefs.h"
#include "tinyxml2\tinyxml2.h"
#include "renderer\CCTexture2D.h"

USING_NS_CC;

namespace Tiled
{

	void PropertySet::load(const tinyxml2::XMLElement* element)
	{
		if (!element) return;

		for (auto propertyElem = element->FirstChildElement("property");
			propertyElem;
			propertyElem = propertyElem->NextSiblingElement("property"))
		{
			_nameToValueMapping[propertyElem->Attribute("name")] = propertyElem->Attribute("value");
		}
	}

	bool PropertySet::has(const std::string& name) const
	{
		return _nameToValueMapping.find(name) != _nameToValueMapping.end();
	}

	std::string PropertySet::get(const std::string& name) const
	{
		auto i = _nameToValueMapping.find(name);
		if (i == _nameToValueMapping.end())
			return "";

		return i->second;
	}


	bool LayerData::load(const tinyxml2::XMLElement* element)
	{
		name = element->Attribute("name");

		size.width = element->IntAttribute("width");
		size.height = element->IntAttribute("height");
		visible = element->Attribute("visible") ? element->BoolAttribute("visible") : true;
		opacity = element->Attribute("opacity") ? element->FloatAttribute("opacity") * 255.0f : 255;

		properties.load(element->FirstChildElement("properties"));

		return true;
	}

}

